package org.example.domain;

import lombok.Data;
import org.example.util.Utils;

import java.io.File;


@Data
public class FileEntity {

    /**
     * 文件名
     */
    private String name;
    /**
     * 文件路径
     */
    private String path;
    /**
     * 绝对路径
     */
    private String absolutePath;
    /**
     * 文件大小(字节)
     */
    private Long size;
    /**
     * 文件大小
     */
    private String sizeStr;
    /**
     * 文件后缀名
     */
    private String suffixName;
    /**
     * 是否是目录
     */
    private boolean directory;
    /**
     * 是否文件
     */
    private boolean file;
    /**
     * 是否盘符
     */
    private boolean drive;
    /**
     * 是否隐藏
     */
    private boolean hidden;

    public FileEntity(File file) {
        this.path = file.getParent();
        this.absolutePath = file.getAbsolutePath();
        this.drive = path == null;
        this.name = this.drive ? file.getAbsolutePath() : file.getName();
        int i = file.getName().lastIndexOf(".");
        if(i > 0) {
            this.suffixName = file.getName().substring( i + 1);
        }
        this.directory = file.isDirectory();
        if(!file.isDirectory()) {
            this.size = file.length();
            this.sizeStr = Utils.byteConversion(file.length());
        }
        this.file = file.isFile();
        this.hidden = file.isHidden();
    }

    public FileEntity(String absolutePath) {
        this(new File(absolutePath));
    }

}
